package com.ruoyi.kanban.controller;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.kanban.domain.DeliveryOrder;
import com.ruoyi.kanban.service.IDeliveryOrderService;

/**
 * 送货单Controller
 */
@RestController
@RequestMapping("/kanban/deliveryOrder")
public class DeliveryOrderController extends BaseController {

    @Autowired
    private IDeliveryOrderService deliveryOrderService;

    /**
     * 查询送货单列表
     */
    @PreAuthorize("@ss.hasPermi('kanban:delivery:list')")
    @GetMapping("/list")
    public TableDataInfo list(
        DeliveryOrder query,
        @RequestParam(value = "beginRequiredDeliveryTime", required = false) String beginRequiredDeliveryTime,
        @RequestParam(value = "endRequiredDeliveryTime", required = false) String endRequiredDeliveryTime
    ) {
        startPage();

        // 传递到货时间范围到 params
        if (beginRequiredDeliveryTime != null || endRequiredDeliveryTime != null) {
            if (query.getParams() == null) {
                query.setParams(new java.util.HashMap<>());
            }
            if (beginRequiredDeliveryTime != null) {
                query.getParams().put("beginRequiredDeliveryTime", beginRequiredDeliveryTime);
            }
            if (endRequiredDeliveryTime != null) {
                query.getParams().put("endRequiredDeliveryTime", endRequiredDeliveryTime);
            }
        }

        List<DeliveryOrder> list = deliveryOrderService.selectDeliveryOrderList(query);
        return getDataTable(list);
    }

    /**
     * 导出送货单列表
     */
    @PreAuthorize("@ss.hasPermi('kanban:delivery:export')")
    @Log(title = "送货单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DeliveryOrder query) {
        List<DeliveryOrder> list = deliveryOrderService.selectDeliveryOrderList(query);
        ExcelUtil<DeliveryOrder> util = new ExcelUtil<>(DeliveryOrder.class);
        util.exportExcel(response, list, "送货单数据");
    }

    /**
     * 获取送货单详细信息
     */
    @PreAuthorize("@ss.hasPermi('kanban:delivery:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(deliveryOrderService.selectDeliveryOrderById(id));
    }

    /**
     * 新增送货单
     */
    @PreAuthorize("@ss.hasPermi('kanban:delivery:add')")
    @Log(title = "送货单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DeliveryOrder entity) {
        return toAjax(deliveryOrderService.insertDeliveryOrder(entity));
    }

    /**
     * 修改送货单
     */
    @PreAuthorize("@ss.hasPermi('kanban:delivery:edit')")
    @Log(title = "送货单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DeliveryOrder entity) {
        return toAjax(deliveryOrderService.updateDeliveryOrder(entity));
    }

    /**
     * 删除送货单
     */
    @PreAuthorize("@ss.hasPermi('kanban:delivery:remove')")
    @Log(title = "送货单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(deliveryOrderService.deleteDeliveryOrderByIds(Arrays.stream(ids).collect(Collectors.toList())));
    }
}